package com.zou.controller.backstage;

import com.zou.commons.constants.Constants;
import com.zou.commons.domain.ReturnObject;
import com.zou.pojo.Article;
import com.zou.pojo.Column;
import com.zou.pojo.Manager;
import com.zou.service.ArticleService;
import com.zou.service.ColumnService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class ArticleController {
    @Autowired
    private ArticleService articleService;
    @Autowired
    private ColumnService columnService;

    /**
     * 携带可用栏目进入文章展示页
     *
     * @param model
     * @return
     */
    @GetMapping("/backstage/article/list")
    public String list(Model model) {
        List<Column> columns = columnService.queryColumnByStateEnabled();
        model.addAttribute("columns", columns);
        return "/backstage/article/list.html";
    }

    /**
     * 携带可用栏目进入文章添加页
     *
     * @param model
     * @return
     */
    @GetMapping("/backstage/article/add")
    public String add(Model model) {
        List<Column> columns = columnService.queryColumnByStateEnabled();
        model.addAttribute("columns", columns);
        return "/backstage/article/add.html";
    }

    /**
     * 添加文章返回状态码
     *
     * @param article
     * @param session
     * @return
     */
    @PostMapping("/backstage/article/add")
    public @ResponseBody
    Object add(Article article, HttpSession session) {
        // 获取当前操作人
        Manager manager = (Manager) session.getAttribute(Constants.SESSION_USER);
        article.setManagerId(manager.getManagerId());
        // 调用service层方法保存Article
        ReturnObject returnObject = new ReturnObject();
        if (articleService.saveArticle(article)) {
            returnObject.setCode(Constants.RETURN_OBJECT_CODE_SUCCESS);
        } else {
            returnObject.setCode(Constants.RETURN_OBJECT_CODE_FAIL);
            returnObject.setMessage("系统繁忙，请稍后重试");
        }
        return returnObject;
    }

    /**
     * 分页查询文章
     *
     * @param state
     * @param column
     * @param name
     * @param pageNo
     * @param pageSize
     * @return
     */
    @PostMapping("/backstage/article/queryColumnByConditionForPage")
    public @ResponseBody
    Object queryColumnByConditionForPage(String state, String column, String name, int pageNo, int pageSize) {
        //封装参数
        Map<String, Object> map = new HashMap<>();
        map.put("state", state);
        map.put("column", column);
        map.put("name", name);
        map.put("beginNo", (pageNo - 1) * pageSize);
        map.put("pageSize", pageSize);
        //调用service层方法，查询数据
        List<Article> articles = articleService.queryArticleByConditionForPage(map);
        int totalRows = articleService.queryCountOfArticleByCondition(map);
        //根据查询结果结果，生成响应信息
        Map<String, Object> retMap = new HashMap<>();
        retMap.put("articles", articles);
        retMap.put("totalRows", totalRows);
        return retMap;
    }

    /**
     * 批量删除文章
     *
     * @param arr
     * @return
     */
    @PostMapping("/backstage/article/del")
    public @ResponseBody
    Object del(String[] arr) {
        ReturnObject returnObject = new ReturnObject();
        if (articleService.deleteArticleByIds(arr)) {
            returnObject.setCode(Constants.RETURN_OBJECT_CODE_SUCCESS);
        } else {
            returnObject.setCode(Constants.RETURN_OBJECT_CODE_FAIL);
            returnObject.setMessage("系统繁忙，请稍后重试");
        }
        return returnObject;
    }

    /**
     * 根据id查询对应文章和栏目，进入修改页面
     *
     * @param id
     * @param model
     * @return
     */
    @GetMapping("/backstage/article/edit/{id}")
    public String edit(@PathVariable String id, Model model) {
        Article article = articleService.queryArticleById(id);
        model.addAttribute("article", article);
        List<Column> columns = columnService.queryColumnByStateEnabled();
        model.addAttribute("columns", columns);
        return "backstage/article/edit.html";
    }

    /**
     * 修改文章信息
     *
     * @param article
     * @return
     */
    @PostMapping("/backstage/article/edit")
    public @ResponseBody
    Object edit(Article article) {
        ReturnObject returnObject = new ReturnObject();
        if (articleService.editArticle(article)) {
            returnObject.setCode(Constants.RETURN_OBJECT_CODE_SUCCESS);
        } else {
            returnObject.setCode(Constants.RETURN_OBJECT_CODE_FAIL);
            returnObject.setMessage("系统繁忙，请稍后重试");
        }
        return returnObject;
    }

    /**
     * 置顶文章
     *
     * @param id
     * @return
     */
    @PostMapping("/backstage/article/sticky")
    public @ResponseBody
    Object sticky(String id) {
        ReturnObject returnObject = new ReturnObject();
        if (articleService.stickyArticle(id)) {
            returnObject.setCode(Constants.RETURN_OBJECT_CODE_SUCCESS);
        } else {
            returnObject.setCode(Constants.RETURN_OBJECT_CODE_FAIL);
            returnObject.setMessage("系统繁忙，请稍后重试");
        }
        return returnObject;
    }

    @PostMapping("/backstage/article/details")
    public @ResponseBody
    Object details(String id) {
        Article article = articleService.queryArticleById(id);
        return article;
    }
}
